<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新备件</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-form ref="form" :model="data" :inline="true" :rules="formRules">
          <el-input v-model="data.id" type="hidden" />
          <el-row :gutter="10">
            <el-col :xl="3" :lg="4" :md="4" :sm="6">
              <el-form-item prop="coding">
                <div>编号</div>
                <el-input v-model="data.coding" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="4" :sm="6">
              <el-form-item prop="name">
                <div>名称</div>
                <el-input v-model="data.name" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="4" :sm="6">
              <el-form-item prop="brand">
                <div>品牌</div>
                <el-input v-model="data.brand" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="4" :sm="6">
              <el-form-item prop="spec">
                <div>型号</div>
                <el-input v-model="data.spec" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="6">
              <el-form-item prop="cost_id">
                <div>成本中心</div>
                <v-select v-model="data.cost_id" action="costList" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="6">
              <el-form-item prop="unit">
                <div>单位</div>
                <el-input v-model="data.unit" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="number">
                <div>当前数量</div>
                <el-input v-model="data.number" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="min_number">
                <div>最小数量</div>
                <el-input v-model="data.min_number" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="2" :lg="4" :md="5" :sm="6">
              <el-form-item prop="max_number">
                <div>最高库存</div>
                <el-input v-model="data.max_number" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="department">
                <div>部门</div>
                <el-select v-model="data.department" size="mini">
                  <el-option v-for="(item, index) in partsDepartment" :key="index" :value="index" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="admin_id">
                <div>负责人</div>
                <v-select v-model="data.admin_id" action="adminList" :query="{category_id:90}" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="4" :sm="6">
              <el-form-item prop="price">
                <div>价格</div>
                <el-input v-model="data.price" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="6">
              <el-form-item prop="category">
                <div>分类</div>
                <v-select v-model="data.category" action="partCategoryList" :paging="false" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="6">
              <el-form-item prop="store_id">
                <div>仓库</div>
                <v-select v-model="data.store_id" action="storeList" :query="{kind:0}" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="6">
              <el-form-item prop="store_location_id">
                <div>库位</div>
                <v-select v-model="data.store_location_id" :disabled="!data.store_id" :query="{store_id:data.store_id}" action="storeLocationlist" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="4" :sm="6">
              <el-form-item prop="status">
                <div>状态</div>
                <el-select v-model="data.status" size="mini">
                  <el-option v-for="(item, index) in commonStatus" :key="index" :value="index" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="4" :sm="6">
              <el-form-item prop="replace_coding">
                <div>可替换编码</div>
                <el-input v-model="data.replace_coding" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="4" :sm="6">
              <el-form-item prop="sort">
                <div>排序</div>
                <el-input v-model="data.sort" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-tabs>
          <el-tab-pane label="适用于资产分类">
            <template>
              <el-table :data="usedEqCategory">
                <el-table-column prop="first_category_name.name" label="一级分类" min-width="20%" />
                <el-table-column prop="category_name.name" label="顶级分类" min-width="21%" />
                <el-table-column prop="notice" label="注意" min-width="34%" />
                <el-table-column label="操作" min-width="8%">
                  <template scope="scope">
                    <el-button type="text" size="small" @click="usedEqCategoryDel(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-form ref="eq_category_form" :model="usedEqCategorySelect" :inline="true" class="hf_form append_other_admin" :rules="eqCategoryRules">
                    <el-row :gutter="10">
                      <el-col :xl="6" :lg="6">
                        <el-form-item prop="first_category">
                          <v-select v-model="usedEqCategorySelect.first_category" action="equipmentCategoryList" :query="{level:0}" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="6" :lg="6">
                        <el-form-item prop="category_id">
                          <v-select v-model="usedEqCategorySelect.category_id" action="equipmentCategoryList" :disabled="!usedEqCategorySelect.first_category" :query="{parent_id:usedEqCategorySelect.first_category}" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="10" :lg="10">
                        <el-form-item prop="notice">
                          <el-input v-model="usedEqCategorySelect.notice" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="2" :lg="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="usedEqCategorySave" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </el-table>
            </template>
          </el-tab-pane>
          <el-tab-pane label="描述">
            <el-row :gutter="10">
              <el-col :span="4">
                <div>
                  <span>创建时间日期</span>
                  <el-input v-model="data.create_time" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div>
                  <span>创建人</span>
                  <el-input v-model="data.cr_admin.name" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div v-if="data.update_time">
                  <div>
                    <span>最近更新</span>
                    <el-input v-model="data.update_time" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div>
                    <span>更新者</span>
                    <el-input v-model="data.up_admin.name" class="mtb10" size="mini" :disabled="true" />
                  </div>
                </div>
              </el-col>
              <el-col :span="16">
                <Tinymce id="content" v-model="data.content" />
              </el-col>
              <el-col :span="4">
                <thumb v-model="data.thumb" />
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="适用于工装">
            <template>
              <el-table :data="usedJig">
                <el-table-column prop="jig.name" label="工装" min-width="20%" />
                <el-table-column prop="jig.manufacturer.name" label="制造商参考编号" min-width="21%" />
                <el-table-column prop="notice" label="注意" min-width="34%" />
                <el-table-column label="操作" min-width="8%">
                  <template scope="scope">
                    <el-button type="text" size="small" @click="usedJigDel(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-form ref="jig_form" :model="usedJigSelect" :inline="true" class="hf_form append_other_admin" :rules="jigRules">
                    <el-row :gutter="10">
                      <el-col :xl="6" :lg="6">
                        <el-form-item prop="jig_id">
                          <v-select-s v-model="usedJigSelect.jig_id" action="equipmentList" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="6" :lg="6">
                        <el-form-item>
                          <el-input :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="10" :lg="10">
                        <el-form-item prop="notice">
                          <el-input v-model="usedJigSelect.notice" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="2" :lg="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="usedJigSave" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </el-table>
            </template>
          </el-tab-pane>
          <el-tab-pane label="供应商">
            <template>
              <el-table :data="provider">
                <el-table-column prop="providers.name" label="供应商" min-width="28%" />
                <el-table-column prop="providers.coding" label="参考编号" min-width="32%" />
                <el-table-column label="是供应商" min-width="10%">
                  <template v-slot="{row}">{{ row.providers.is_provider === 1?'是':'否' }}</template>
                </el-table-column>
                <el-table-column label="是制造商" min-width="10%">
                  <template slot-scope="{row}">{{ row.providers.is_manufacturer === 1?'是':'否' }}</template>
                </el-table-column>
                <el-table-column label="是外包商" min-width="10%">
                  <template slot-scope="{row}">{{ row.providers.is_epiboly === 1? '是':'否' }}</template>
                </el-table-column>
                <el-table-column label="操作" min-width="7%">
                  <template scope="scope">
                    <el-button type="text" size="small" @click="providerDel(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-form ref="append_provider" :model="providerSelect" :inline="true" class="hf_form append_other_admin" :rules="appendProvider">
                    <el-row :gutter="10">
                      <el-col :xl="7" :lg="7">
                        <el-form-item prop="provider_id">
                          <v-select-s v-model="providerSelect.provider_id" action="ProviderList" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="8" :lg="8">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="2" :lg="2">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="3" :lg="3">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="2" :lg="2">
                        <el-form-item>
                          <el-input v-model="providerSelect.coding" :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="2" :lg="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="providerSave" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </el-table>
            </template>
          </el-tab-pane>
          <el-tab-pane label="适用于资产">
            <template>
              <el-table :data="usedEq">
                <el-table-column prop="eq.name" label="资产" min-width="20%" />
                <el-table-column prop="eq.manufacturer.name" label="制造商参考编号" min-width="21%" />
                <el-table-column prop="notice" label="注意" min-width="34%" />
                <el-table-column label="操作" min-width="8%">
                  <template scope="scope">
                    <el-button type="text" size="small" @click="usedEqDel(scope.row.id)">删除</el-button>
                  </template>
                </el-table-column>
                <div slot="append">
                  <el-form ref="eq_form" :model="usedEqSelect" :inline="true" class="hf_form append_other_admin" :rules="eqRules">
                    <el-row :gutter="10">
                      <el-col :xl="6" :lg="6">
                        <el-form-item prop="equipment_id">
                          <v-select-s v-model="usedEqSelect.equipment_id" action="eqList" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="6" :lg="6">
                        <el-form-item>
                          <el-input :disabled="true" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="10" :lg="10">
                        <el-form-item prop="notice">
                          <el-input v-model="usedEqSelect.notice" size="mini" />
                        </el-form-item>
                      </el-col>
                      <el-col :xl="2" :lg="2">
                        <el-form-item>
                          <el-button icon="el-icon-upload2" size="mini" @click="usedEqSave" />
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
              </el-table>
            </template>
          </el-tab-pane>
          <el-tab-pane label="交易">
            <el-button type="text">入库记录</el-button>
            <el-table :data="enter_list">
              <el-table-column label="入库单">
                <template v-slot="{row}">
                  {{ row.enter_order.coding }}-{{ row.enter_order.name }}
                </template>
              </el-table-column>
              <el-table-column label="验证人">
                <template v-slot="scope">
                  <div v-for="(item, adminIndex) in scope.row.approve_admins" :key="adminIndex">
                    <span>{{ item.admin.true_name }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="数量" prop="number" />
              <el-table-column label="单价" prop="price" />
              <el-table-column label="总价">
                <template v-slot="{row}">
                  {{ row.price * row.number }}
                </template>
              </el-table-column>
              <el-table-column label="供应商" prop="number" />
              <el-table-column label="日期" prop="create_time" />
            </el-table>
            <el-button type="text">出库记录</el-button>
            <el-table :data="appear_list">
              <el-table-column label="出库单" min-width="30%">
                <template v-slot="{row}">
                  {{ row.appear_order.coding }}-{{ row.appear_order.name }}
                </template>
              </el-table-column>
              <el-table-column label="验证人" prop="appear_order.verify_admins.name" min-width="10%" align="center" />
              <el-table-column label="验证日期" prop="appear_order.verify_date" min-width="20%" align="center" />
              <el-table-column label="申请人" prop="appear_order.apply_admins.name" min-width="10%" align="center" />
              <el-table-column label="工单" min-width="30%">
                <template slot-scope="scope">
                  {{ scope.row.appear }}
                </template>
              </el-table-column>
            </el-table>
            <el-button type="text">退还记录</el-button>
            <el-table :data="back_list">
              <el-table-column label="退还单">
                <template v-slot="{row}">
                  {{ row.back_order.coding }}-{{ row.back_order.name }}
                </template>
              </el-table-column>
              <el-table-column label="数量" prop="number" />
              <el-table-column label="验证人">
                <template v-slot="{row}">
                  {{ row.back_order.verify_admins.name }}
                </template>
              </el-table-column>
              <el-table-column label="验证日期">
                <template v-slot="{row}">
                  {{ row.back_order.verify_date }}
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="计划的交易">
            <el-table :data="plan_list">
              <el-table-column label="安排时间日期" prop="work_orders.arrange_date" />
              <el-table-column label="资产" prop="eq.name" />
              <el-table-column label="工单">
                <template v-slot="{row}">
                  {{ row.work_orders.coding }}-{{ row.work_orders.name }}
                </template>
              </el-table-column>
              <el-table-column label="计划数量" prop="number" />
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/stock/parts'
import { commonStatus, partsDepartment } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
import thumb from '@/components/Upload/thumb'
import components from '@/components/store/part/index'
export default {
  inject: ['backUrl', 'goUrl', 'delOne', 'update'],
  components: {
    Tinymce,
    VSelect,
    VSelectS,
    thumb,
    ...components
  },
  data() {
    return {
      data: {
        'cr_admin': {
          'name': ''
        }
      },
      usedEq: [],
      usedEqCategory: [],
      usedJig: [],
      usedEqSelect: {},
      usedJigSelect: {},
      usedEqCategorySelect: {},
      provider: [],
      enter_list: [],
      appear_list: [],
      back_list: [],
      plan_list: [],
      providerSelect: {},
      commonStatus,
      partsDepartment,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入备件名称',
            trigger: 'blur'
          }
        ],
        brand: [
          {
            required: true,
            message: '请输入品牌',
            trigger: 'blur'
          }
        ],
        spec: [
          {
            required: true,
            message: '请输入型号',
            trigger: 'blur'
          }
        ],
        cost_id: [
          {
            required: true,
            message: '请选择成本中心',
            trigger: 'change'
          }
        ],
        unit: [
          {
            required: true,
            message: '请输入单位',
            trigger: 'blur'
          }
        ],
        number: [
          {
            required: true,
            message: '请输入数量',
            trigger: 'blur'
          }
        ],
        min_number: [
          {
            required: true,
            message: '请输入最小数量',
            trigger: 'blur'
          }
        ],
        max_number: [
          {
            required: true,
            message: '请输入最高库存',
            trigger: 'blur'
          }
        ],
        department_id: [
          {
            required: true,
            message: '请选择部门',
            trigger: 'change'
          }
        ],
        price: [
          {
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          }
        ],
        category: [
          {
            required: true,
            message: '请选择分类',
            trigger: 'change'
          }
        ],
        store_id: [
          {
            required: true,
            message: '请选择仓库',
            trigger: 'change'
          }
        ],
        store_location_id: [
          {
            required: true,
            message: '请选择库位',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      appendProvider: {
        provider_id: [
          {
            required: true,
            message: '请选择供应商',
            trigger: 'change'
          }
        ]
      },
      eqRules: {
        equipment_id: [
          {
            required: true,
            message: '请选择资产',
            trigger: 'change'
          }
        ]
      },
      eqCategoryRules: {
        first_category: [
          {
            required: true,
            message: '请选择一级分类',
            trigger: 'change'
          }
        ],
        category_id: [
          {
            required: true,
            message: '请选择顶级分类',
            trigger: 'change'
          }
        ]
      },
      jigRules: {
        jig_id: [
          {
            required: true,
            message: '请选择工装',
            trigger: 'change'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.data.error_code === 42404) {
        this.$alert('您请求的备件不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      // response.data.used_eq.forEach((item) => {
      //   if (item.eq.gz === 0) {
      //     this.usedEq.push(item)
      //   }
      //   if (item.eq.gz === 1) {
      //     this.usedJig.push(item)
      //   }
      // })
      this.usedEq = response.data.used_eq
      this.usedJig = response.data.used_jig
      this.enter_list = response.data.enter_list
      this.appear_list = response.data.appear_list
      this.back_list = response.data.back_list
      this.provider = response.data.provider
      this.usedEqCategory = response.data.used_equipment_category
      this.plan_list = response.data.plan_list
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    usedEqSave() {
      this.$refs.eq_form.validate((valid) => {
        if (valid) {
          const equipment_id = this.usedEqSelect.equipment_id
          const notice = this.usedEqSelect.notice
          const used_data = {
            'equipment_id': equipment_id,
            'notice': notice,
            'parts_id': this.$route.params.id
          }
          API.usedEq(used_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.usedEq = response.data.used_eq
              })
            } else if (response.data.error_code === 42004) {
              this.$message.error('该设备已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    usedEqDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delUsedEq(id).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.usedEq = response.data.used_eq
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    usedEqCategorySave() {
      this.$refs.eq_category_form.validate((valid) => {
        if (valid) {
          const used_category_data = {
            'first_category': this.usedEqCategorySelect.first_category,
            'category_id': this.usedEqCategorySelect.category_id,
            'notice': this.usedEqCategorySelect.notice,
            'parts_id': this.$route.params.id
          }
          API.usedEqCategory(used_category_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(res => {
                this.usedEqCategory = res.data.used_eq_category
              })
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    usedEqCategoryDel(id) {
      const del_parts_category_data = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delUsedEqCategory(del_parts_category_data).then((response) => {
          console.log(response)
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(res => {
              this.usedEqCategory = res.data.used_eq_category
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    usedJigSave() {
      this.$refs.jig_form.validate((valid) => {
        if (valid) {
          const jig_id = this.usedJigSelect.jig_id
          const notice = this.usedJigSelect.notice
          const used_data = {
            'jig_id': jig_id,
            'notice': notice,
            'parts_id': this.$route.params.id
          }
          API.usedJig(used_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.usedJig = response.data.used_jig
              })
            } else if (response.data.error_code === 42004) {
              this.$message.error('该工装已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    usedJigDel(id) {
      const del_parts_data = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delUsedJig(del_parts_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.usedJig = response.data.used_jig
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    providerSave() {
      this.$refs.append_provider.validate((valid) => {
        if (valid) {
          const provider_id = this.providerSelect.provider_id
          const parts_provider = {
            'provider_id': provider_id,
            'parts_id': this.$route.params.id
          }
          API.partsProvider(parts_provider).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.provider = response.data.provider
              })
            } else if (response.data.error_code === 42004) {
              this.$message.error('该供应商已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    providerDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delPartsProvider(id).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.provider = response.data.provider
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    goBack() {
      this.goUrl('/stock/parts')
    }
  }
}
</script>
